from __future__ import print_function, division
import os
import sys
import subprocess
import shutil
import cv2

# split_dir = '../data/testTrainMulti_7030_splits/'
# data_dir = '../data/HMDB51/'
# dst_dir = '../data/hmdb-51/'
# txts = os.listdir(split_dir)

# if not os.path.exists(dst_dir):
#     os.mkdir(dst_dir)
    
# for i in range(1):
#     split = 'split{}'.format(i+1)
#     dst_dir1 = dst_dir+'split{}/'.format(i+1)
#     if not os.path.exists(dst_dir1):
#         os.mkdir(dst_dir1)
#     dst_train_dir = dst_dir1 + 'train/'
#     if not os.path.exists(dst_train_dir):
#         os.mkdir(dst_train_dir)
#     dst_test_dir = dst_dir1 + 'test/'
#     if not os.path.exists(dst_test_dir):
#         os.mkdir(dst_test_dir)   

#     for txt in txts:
#         if txt[:-4].endswith(split):
#             act = txt[:-16]
#             act_dst_train_dir = dst_train_dir + act + '/'
#             act_dst_test_dir = dst_test_dir + act + '/'
#             if not os.path.exists(act_dst_train_dir):
#                 os.mkdir(act_dst_train_dir)  
#             if not os.path.exists(act_dst_test_dir):
#                 os.mkdir(act_dst_test_dir)  
             
#             with open(split_dir + txt, 'r') as f:
#                 lines = f.readlines()
#                 for line in lines:
#                     filename, mode = line.split(' ')[0], line.split(' ')[1]
#                     src = data_dir + act + '/' + filename
#                     if mode == '1':
#                         dst = act_dst_train_dir + filename
#                     elif mode == '2':
#                         dst = act_dst_test_dir + filename
#                     shutil.copyfile(src, dst)
            
def class_process(dir_path, dst_dir_path, class_name, maxSize=1024):
    class_path = os.path.join(dir_path, class_name)
    if not os.path.isdir(class_path):
        return

    dst_class_path = os.path.join(dst_dir_path, class_name)
    if not os.path.exists(dst_class_path):
        os.makedirs(dst_class_path)

    for file_name in os.listdir(class_path):
        if '.avi' not in file_name:
            continue
        name, ext = os.path.splitext(file_name)
        dst_directory_path = os.path.join(dst_class_path, name)

        video_file_path = os.path.join(class_path, file_name)

        # skip large files
        # if os.path.getsize(video_file_path) > maxSize * 1000:
        #	continue

        try:
            if os.path.exists(dst_directory_path):
                if not os.path.exists(os.path.join(dst_directory_path, 'image_00001.jpg')):
                    subprocess.call('rm -r \"{}\"'.format(dst_directory_path), shell=True)
                    print('remove {}'.format(dst_directory_path))
                    os.makedirs(dst_directory_path)
                else:
                    continue
            else:
                os.makedirs(dst_directory_path)
                print('making ', dst_directory_path)
        except:
            print(dst_directory_path)
            continue

        # cmd = 'ffmpeg -i \"{}\" -vf scale=-1:240 \"{}/image_%05d.jpg\"'.format(video_file_path, dst_directory_path)
        cmd = 'ffmpeg -i \"{}\" -qscale:v 2 \"{}/image_%05d.jpg\"'.format(video_file_path, dst_directory_path)
        
        subprocess.call(cmd, shell=True)

train_paths = ['../data/hmdb-51/split{}/train/'.format(i) for i in range(1,4)]
test_paths = ['../data/hmdb-51/split{}/test/'.format(i) for i in range(1,4)]
dst_train_paths = ['../data/hmdb---51/split{}/train/'.format(i) for i in range(1,4)]
dst_test_paths = ['../data/hmdb---51/split{}/test/'.format(i) for i in range(1,4)]
for i in range(1):
    for class_name in os.listdir(train_paths[i]):
        class_process(train_paths[i], dst_train_paths[i], class_name)
    for class_name in os.listdir(test_paths[i]):
        class_process(test_paths[i], dst_test_paths[i], class_name)
    break

data_dir = '../data/hmdb---51/split1/'

for rootname, dirname, filenames in os.walk(data_dir):
    for filename in filenames:
        img_file = os.path.join(rootname, filename)
        img = cv2.imread(img_file)
        img = cv2.resize(img, (320, 240))
        cv2.imwrite(img_file, img)